package my2Cents;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.phys2d.raw.Body;
import net.phys2d.raw.Joint;
import net.phys2d.raw.World;

public class WorldLoader extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2597863048969676757L;
	
	private JList files;
	private JSplitPane split;
	private JSplitPane split2;
	private JScrollPane clistpane;
	private JPanel previewPane;
	private JLabel previewLabel;
	private File selected;
	private boolean accepted = false;
	private static BufferedImage errorIcon = IconLoader.loadIcon(new File("data/images/error_big.jpg"));
	
	public WorldLoader(Window owner, String title, ModalityType type) {
		super(owner,title,type);
		this.setSize(500, 400);
		//this.setLayout(new BorderLayout());
		
		previewPane = new JPanel();
		previewPane.setPreferredSize(new Dimension(256,256));
		
		previewLabel = new JLabel("<Filename>");
		previewPane.add(previewLabel);
		
		split2 = new JSplitPane(
				JSplitPane.VERTICAL_SPLIT,
				previewPane,
				okButton());
		
		split = new JSplitPane(
				JSplitPane.HORIZONTAL_SPLIT, 
				setupFileList(),
				split2);
		
		this.getContentPane().add(split);
		
		split.setResizeWeight(1);
		split.setEnabled(false);
		
		split2.setResizeWeight(1);
		split2.setEnabled(false);
		
		addFiles();
		
		split.repaint();
	}
	
	public File getSelectedFile() {
		if(accepted) {
			return selected;
		} else {
			return null;
		}
	}
	
	private void closeMe() {
		this.setVisible(false);
		this.dispose();
		this.setModalityType(ModalityType.MODELESS);
	}
	
	private Component okButton() {
		JButton ok = new JButton("Load World");
		ok.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				accepted = true;
				closeMe();
			}
			
		});
		return ok;
	}

	
	public void addFiles() {
		Vector<File> rez = Util_FILE.recursiveFind(".world_" + Sandbox.VERSION, new File("data/worlds"));
		files.setListData(rez);
		if(rez.size() == 0) {
			JOptionPane.showMessageDialog(this, "No world files found!", "Error", JOptionPane.ERROR_MESSAGE);
			closeMe();
		}
	}
	
	private void error(Graphics g) {
		g.drawImage(errorIcon, 
				(previewPane.getWidth()/2) - 128, 
				(previewPane.getHeight()/2) - 128, 256, 256, null);
		g.drawString("World File Out Of Date!", 5, 50);
	}

	private JScrollPane setupFileList() {
		files = new JList();
		files.setLayout(null);
		
		files.addListSelectionListener(new ListSelectionListener() {

			public void valueChanged(ListSelectionEvent arg0) {
				File value = (File) files.getSelectedValue();
				selected = value;
				previewLabel.setText(selected.getName());
				
				Graphics2D g = (Graphics2D) previewPane.getGraphics();
				previewPane.paint(g);
				
				World temp = Util.loadWorld(value);
				
				if(temp == null) {error(g); return;}
				if(temp.getBodies() == null) {error(g); return;}
				
				AffineTransform before = g.getTransform();
				AffineTransform scaletoworld = new AffineTransform();
				
				scaletoworld.scale(0.3, 0.3);
				g.setTransform(scaletoworld);
				
				for(int i=0; i<temp.getBodies().size(); i++) {
					Body b = temp.getBodies().get(i);
					b.draw(g);
				}
			
				for(int i=0; i<temp.getJoints().size(); i++) {
					Joint j = temp.getJoints().get(i);
					DrawingUtilities.drawJoint(g, j);
				}
				
				g.setTransform(before);
			}
			
		});
		
		clistpane = new JScrollPane(files);
		clistpane.setVerticalScrollBarPolicy(
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		clistpane.setHorizontalScrollBarPolicy(
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		clistpane.setPreferredSize(new Dimension(150,100));
		
		return clistpane;
	}
	
	public static void main(String[] args) {
		WorldLoader l = new WorldLoader(null, "test", ModalityType.APPLICATION_MODAL);
		l.setVisible(true);
		System.out.println("Finished");
		System.out.println(l.getSelectedFile());
		System.exit(0);
	}
	
}
